package com.fr.data.impl.sap;

import com.fr.general.ComparatorUtils;
import com.fr.general.xml.GeneralXMLTools;
import com.fr.stable.xml.XMLConstants;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLable;
import com.fr.stable.xml.XMLableReader;

public class SAPTransferColumnConfig implements XMLable {
    public static final String XML_TAG = "SAPCC";

    private boolean isKey;
    private Object value;
    private String columnName;

    public SAPTransferColumnConfig() {
    }

    public SAPTransferColumnConfig(boolean isKey, Object value,
                                   String columnName) {
        super();
        this.isKey = isKey;
        this.value = value;
        this.columnName = columnName;
    }

    public boolean isKey() {
        return isKey;
    }

    public void setKey(boolean isKey) {
        this.isKey = isKey;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public void readXML(XMLableReader reader) {
        String tmpVal = null;
        if (reader.isAttr()) {
            if ((tmpVal = reader.getAttrAsString("name", null)) != null) {
                this.columnName = tmpVal;
            }
            this.isKey = reader.getAttrAsBoolean("isKey", false);
        }

        if (reader.isChildNode()) {
            String tmpName = reader.getTagName();
            Object value;
            if (XMLConstants.OBJECT_TAG.equals(tmpName)) {
                value = GeneralXMLTools.readObject(reader);
            } else {
                value = "";
            }
            this.setValue(value);
        }
    }

    public void writeXML(XMLPrintWriter writer) {
        writer.startTAG(XML_TAG).attr("name", this.columnName).attr("isKey", this.isKey);
        GeneralXMLTools.writeObject(writer, value);

        writer.end();
    }

    public boolean equals(Object obj) {
        return obj instanceof SAPTransferColumnConfig
                && ((SAPTransferColumnConfig) obj).isKey == isKey
                && ComparatorUtils.equals(((SAPTransferColumnConfig) obj).columnName, columnName)
                && ComparatorUtils.equals(((SAPTransferColumnConfig) obj).value, value);
    }

    public Object clone() throws CloneNotSupportedException {
        SAPTransferColumnConfig newColumnConfig = (SAPTransferColumnConfig) super.clone();
        return newColumnConfig;
    }
}
